<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_managements extends Admin_Controller {
    //var $access_supperadmin = 1;
    function User_managements() {
        parent::Admin_Controller();
        $CI = & get_instance();
        $CI->load->module_model('user_managements.user_management');
        $CI->load->module_define('administrator.user_managements');
        $CI->load->module_language('user_managements.common');
    }

    function index() {
        $this->lists();
    }

    function lists() {
        $CI = & get_instance();

        //
        //var_dump($CI->load->user_management_type);
        $limit = $CI->config->item('rows_per_page');
        list($offset_segment, $sort_segment, $sorttype_segment) = array(3, 4, 5);
        $offset = $CI->uri->segment($offset_segment)?$CI->uri->segment($offset_segment):0;

        $sort_field = $CI->uri->segment($sort_segment)?$CI->uri->segment($sort_segment):'name';

        $user_managements = $CI->user_management->get_lists(NULL, $limit, $offset, $sort_field, $CI->uri->segment($sorttype_segment));

        $this->add('user_managements', $user_managements);

        $base_url = 'admin/user_managements.user_managements';
        $CI->load->library('pagination');
        $config['uri_segment'] = $offset_segment;
        $config['base_url'] = site_url($base_url.'/%s/'.$sort_field.'/'.$CI->uri->segment($sorttype_segment));
        $config['total_rows'] = $CI->user_management->get_numrows();
        $config['per_page'] = $limit;
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());

        $this->add('NameTX', $this->sort_column('Họ tên', 'name', $CI->uri->segment($sorttype_segment), $base_url.'/'.$offset, $sort_field));
        $this->add('LoginName', $this->sort_column('Tên đăng nhập', 'login_name', $CI->uri->segment($sorttype_segment), $base_url.'/'.$offset, $sort_field));
        $this->add('SDTX', $this->sort_column('Ngày tạo', 'start_date', $CI->uri->segment($sorttype_segment), $base_url.'/'.$offset, $sort_field));
        $this->add('ID', $this->sort_column('ID', 'id', $CI->uri->segment($sorttype_segment), $base_url.'/'.$offset, $sort_field));


        $this->module_navigation(lang('_title'));
        $this->module_toolbar('user_managements.toolbar');
        $this->view('lists');
    }

    function edit() {
        $CI = & get_instance();
        $admin_id = $CI->uri->segment(3);
        if(!$admin_id) {
            set_flash_message(lang('_edit_not_found'));
            redirect('admin/user_managements.user_managements');
        }
        $CI->user_management->set_id($admin_id);
        if(!$CI->user_management->get()) {
            set_flash_message(lang('_edit_not_found'));
            redirect('admin/user_managements.user_managements');
        }
        if($this->_validate() !== FALSE) {
            if($this->_save_user_management($admin_id)) {
                set_flash_message(lang('_edit_success'));
                redirect('admin/user_managements.user_managements/');
            }
        }
        $this->add('name', isset($_POST['name'])?$CI->input->post('name'):$CI->user_management->name);
        $this->add('login_name', isset($_POST['login_name'])?$CI->input->post('login_name'):$CI->user_management->login_name);
        $this->module_navigation(lang('_title').' : '.lang('_edit_title'));
        $this->view('add_edit');
    }

    function add_new() {
        $CI = & get_instance();

        if($this->_validate(TRUE) !== FALSE) {
            if($this->_save_user_management()) {
                set_flash_message(lang('_add_success'));
                redirect('admin/user_managements.user_managements/');
            }
        }

        $this->add('name', $CI->input->post('name'));
        $this->add('login_name', $CI->input->post('login_name'));        
        $this->add('add_new', 1);
        $this->module_navigation(lang('_title').' : '.lang('_add_title'));
        $this->view('add_edit');
    }


    function delete() {
        $CI = & get_instance();
        $admin_id = $CI->uri->segment(3);
        if(!$admin_id) {
            set_flash_message(lang('_edit_not_found'));
            redirect('admin/user_managements.user_managements');
        }
        $CI->user_management->set_id($admin_id);
        if(!$CI->user_management->get()) {
            set_flash_message(lang('_edit_not_found'));
            redirect('admin/user_managements.user_managements');
        }

        $CI->user_management->delete();
        set_flash_message(lang('_delete_success'));
        redirect('admin/user_managements.user_managements');
    }

    function _validate($is_addnew = FALSE) {
        $CI = & get_instance();
        $CI->load->library('form_validation');
        $CI->load->helper('form');

        $CI->form_validation->set_rules('name', 'Name', 'trim|required');
        $CI->form_validation->set_rules('login_name', 'Franchisee', 'trim|required');
        if($is_addnew) {
            $CI->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
            $CI->form_validation->set_rules('re_password', 'Re-type Password', 'trim|required|matches[password]');
        }else {
            $CI->form_validation->set_rules('re_password', 'Re-type Password', 'matches[password]');
        }
        return $CI->form_validation->run();
    }

      

    function _save_user_management($admin_id = NULL) {
        $CI = & get_instance();
        if($CI->user_management->check_login_name($admin_id, $CI->input->post('login_name'))) {
            $this->add('error_msg', lang('_name_duplicate'));
            return FALSE;
        }
        $f_data = array(
            ADMIN_NAME => $CI->input->post('name'),
            LOGIN_NAME => $CI->input->post('login_name')
        );
        // If edit
        if(!empty($admin_id)) {
            $new_pass = $CI->input->post('password');
            if(!empty($new_pass))
                $f_data[ADMIN_ADMIN_PASSWORD] = md5($new_pass);
        // Add new
        }else
            $f_data[ADMIN_ADMIN_PASSWORD] = md5($CI->input->post('password'));

        $admin_id = $CI->user_management->save($admin_id, $f_data);        
        
        //$CI->load->module_model('front.configurations.config_model');
        //$CI->config_model->admin_id = $admin_id;       
        return $admin_id;
    }
	/*
    function migrate() {
        $CI = & get_instance();
        $CI->load->library('form_validation');
        $CI->load->helper('form');

        $CI->form_validation->set_rules('fid', 'Franchise', 'trim|required|is_natural');

        if ($CI->form_validation->run() !== FALSE) {
            $to_fid = $CI->user_management->migrate($CI->input->post('to_fid'));
            
            $CI->load->module_helper('administrator.user_managements.theme_xml');
            $CI->load->module_model('front.configurations.config_model');
            $CI->config_model->admin_id = $to_fid;
            $presentation_template = $CI->config_model->get_config(PRESENTATION_TEMPLATES);
            
            $CI->load->module_model('administrator.user_managements.user_management');
            $CI->user_management->admin_id = $to_fid;
            $CI->user_management->get_by_fid();

            generate_theme_xml($to_fid, $CI->user_management->user_management_title, $CI->user_management->user_management_type, empty($presentation_template->value)?'morning':$presentation_template->value);
            
            set_flash_message(lang('_migrate_success').' New user_management is '.$to_fid);
            redirect('admin/user_managements.user_managements.migrate/');
        }

        $user_managementes = $CI->user_management->get_lists(NULL, NULL, NULL, 'id');
        foreach(array_keys($user_managementes) as $k) {
            if($CI->user_management->has_data($user_managementes[$k]->id)) {
                unset($user_managementes[$k]);
                continue;
            }

            $user_managementes[$k]->selected = '';
            if($CI->input->post('to_fid') == $user_managementes[$k]->id)
                $user_managementes[$k]->selected = 'selected';
        }
        $this->add('user_managementes', $user_managementes);
        $this->module_navigation(lang('_title').' : '.lang('_migrate_title'));
    }

    function export() {
        $CI = & get_instance();
        $CI->load->module_define('front.configurations.config_model');
        $CI->user_management->export();
        
        $this->view('export');
    }
    
    function send_mails(){
        $CI = & get_instance();
        $this->module_navigation(lang('_title_send_mail'));
        
        $CI = & get_instance();
        $CI->load->library('form_validation');
        $CI->load->helper('form');
        $CI->form_validation->set_rules('message', 'Message', 'trim|required');
        $CI->form_validation->set_rules('subject', 'Subject', 'trim|required');
        if($CI->form_validation->run()){
            $CI->load->library('letter');
            $CI->letter->subject = $CI->input->post('subject');
            $CI->letter->message = $CI->input->post('message');
            $user_managements = $CI->user_management->get_lists(NULL, NULL, NULL, 'id');
            $result_str = 'Email sent to :<br><ul>';
            foreach($user_managements as $f){
                $CI->load->module_model('administrator.locations.address', 'address_model');
                $CI->address_model->set_id($f->address_id);
                $CI->address_model->admin_id = $f->id;
                $CI->address_model->get_by_fid();
                if(!empty($CI->address_model->email)){
                    $CI->letter->add("user_managemente_name", $f->name);
                    $CI->letter->send('Website@Gymbaroo.biz', $CI->address_model->email);
                    //$CI->letter->send('luanvan83@vnn.vn', $CI->address_model->email);
                    $result_str .= '<li>'.$CI->address_model->email.'</li>';
                }
            }
            $result_str .= '</ul>';
            $this->add('send_ok', $result_str);
        }
        $CI->load->library('fckeditor');
        $CI->fckeditor->SetPathOfFranchise(1);
        $CI->fckeditor->InstanceName = 'message';
        $CI->fckeditor->ToolbarSet = 'term';
        $CI->fckeditor->Value = $CI->input->post('message');
        $message = $CI->fckeditor->CreateHtml();

        $this->add('message', $message);
        $this->add('subject', $CI->input->post('subject'));
    }
	*/
}